﻿using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using Clipping.Infrastructure.Interfaces;
using Clipping.Infrastructure.Models;
namespace Clipping.SearchUrlConstructors
{
	public class BaiduNewsUrlConstructor : ISearchUrlConstruct
	{
		protected static Regex timespanRegex = new Regex("最近(\\d+)(小时|天|周|月|年)");
		private static DateTime baseDate = DateTime.Parse("1970-1-1 8:00");
		private Encoding _urlEncoding = Encoding.GetEncoding("gb2312");

		public Uri ConstructUrl(SearchObject searchObj)
		{
			//Contract.Requires(searchObj != null);

			StringBuilder sb = new StringBuilder(500);
			sb.Append("http://news.baidu.com/ns?cl=2&rn=20");
			sb.AppendFormat("&q1={0}", HttpUtility.UrlEncode(searchObj.Keyword, this._urlEncoding));

			if (searchObj is PeriodSearchObject)
			{
				PeriodSearchObject so = searchObj as PeriodSearchObject;
				DateTime today = DateTime.Now;
				if(so.SearchPeriod== SearchPeriod.全部时间)
				{
						if (so.SearchType == SearchType.SearchInContent)
						{
							sb.Append("&tn=news&ct=0");
						}
						else
						{
							sb.Append("&tn=newstitle&ct=0");
						}
				}
				else if(so.SearchPeriod == SearchPeriod.最近1天 || so.SearchPeriod == SearchPeriod.最近1周 || so.SearchPeriod == SearchPeriod.最近1月 )
				{
					if (so.SearchType == SearchType.SearchInContent)
					{
						sb.Append("&tn=newsdy");
					}
					else
					{
						sb.Append("&tn=newstitledy");
					}

					sb.AppendFormat("&bt={0}", 0);
					sb.AppendFormat("&y0={0}", today.Year);
					sb.AppendFormat("&m0={0}", today.Month);
					sb.AppendFormat("&d0={0}", today.Day);
					sb.AppendFormat("&y1={0}", today.Year);
					sb.AppendFormat("&m1={0}", today.Month);
					sb.AppendFormat("&d1={0}", today.Day);
					sb.AppendFormat("&et={0}", 0);
					sb.AppendFormat("&begin_date={0:yyyy-M-d}", today);
					sb.AppendFormat("&end_date={0:yyyy-M-d}", today);
					int hours = getHours(so.SearchPeriod.ToString());
					sb.AppendFormat("&mt={0}",hours);
					sb.AppendFormat("&lm={0}", hours);
					sb.Append("&s=1&ct1=0&ct=0&class=0&q6=");

				}
				else
				{
					if (so.SearchType == SearchType.SearchInContent)
					{
						sb.Append("&tn=newsdy");
					}
					else
					{
						sb.Append("&tn=newstitledy");
					}
					
					int hours = getHours(so.SearchPeriod.ToString());
					DateTime startTime=today.AddHours(0-hours);
					sb.Append(getSearchTimeQueryString(startTime,today));
				}
			}
			else if (searchObj is TimespanSearchObject)
			{
				TimespanSearchObject so = searchObj as TimespanSearchObject;
				sb.Append(getSearchTimeQueryString(so.StartTime, so.EndTime));
				if (so.SearchType == SearchType.SearchInContent)
					sb.Append("&tn=newsdy&ct1=0&ct=0&class=0&q6=");
				else
					sb.Append("&tn=newstitledy&ct=0&class=0&q6=");
			}

			return new Uri(sb.ToString());
		}
		private string getSearchTimeQueryString(DateTime startTime, DateTime endTime)
		{
			long bt = (long)((startTime - baseDate).TotalSeconds);
			long et = (long)((endTime - baseDate).TotalSeconds) + 86399;
			StringBuilder sb = new StringBuilder(400);
			sb.AppendFormat("&bt={0}", bt);
			sb.AppendFormat("&y0={0}", startTime.Year);
			sb.AppendFormat("&m0={0}", startTime.Month);
			sb.AppendFormat("&d0={0}", startTime.Day);
			sb.AppendFormat("&y1={0}", endTime.Year);
			sb.AppendFormat("&m1={0}", endTime.Month);
			sb.AppendFormat("&d1={0}", endTime.Day);
			sb.AppendFormat("&et={0}", et);
			sb.AppendFormat("&begin_date={0:yyyy-M-d}", startTime);
			sb.AppendFormat("&end_date={0:yyyy-M-d}", endTime);
			return sb.ToString();
		}
		private int getHours(string timeSpan)
		{
			if (timeSpan == null)
			{
				return 24;
			}
			else
			{
				Match m = timespanRegex.Match(timeSpan);

				if (m == null)
				{
					return 24;
				}
				else
				{
					switch (m.Groups[2].Value)
					{
						case "小时":
							return int.Parse(m.Groups[1].Value);
						case "天":
							return int.Parse(m.Groups[1].Value) * 24;
						case "周":
							return int.Parse(m.Groups[1].Value) * 168;
						case "月":
							return int.Parse(m.Groups[1].Value) * 720;
						case "年":
							return int.Parse(m.Groups[1].Value) * 8640;
						default:
							return 24;
					}
				}
			}
		}
	
		public Uri ConstructAbsoluteUrl(Uri baseUri, string url)
		{
			return UriHelper.ConstructAbsoluteUrl(baseUri, url);
		}
	}
}
